package scigest.configuration;

import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.martiansoftware.jsap.ParseException;
import com.martiansoftware.jsap.StringParser;

/**
 * 
 * Sub-command validation class
 * 
 * @author Feiyi Wang
 *
 */
public class CmdParser extends StringParser {
	private static final Logger logger = LoggerFactory.getLogger(CmdParser.class);
	private static final String[] cmdList = new String[] {
		ScigestCmd.CMD_GET,
		ScigestCmd.CMD_LIST,
		ScigestCmd.CMD_RESTORE,
		ScigestCmd.CMD_SET
		};
	
	@Override
	public Object parse(String cmd) throws ParseException {
		
		if (contains(cmd)) 
			return cmd;
		else {
			logger.error("[{}]  is an unknown command", cmd);
			logger.error("Hint: a subcommand must be one of: " + Arrays.toString(cmdList));
			return null;
		}
	}

	private boolean contains(String cmd) {
		for (int i = 0; i < cmdList.length; i++) {
			if (cmdList[i].equals(cmd)) return true;
		}
		return false;
	}
}
